package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.firstsqlserver.ChartMapper;
import com.example.demo.pojo.Chart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ChartController {

    @Autowired
    private ChartMapper chartMapper;

    @RequestMapping("/chart")
    public String chart(Model model) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(new Date());
        List<Chart> charts = chartMapper.listChart(now);
        String datas = JSON.toJSONString(charts);
        model.addAttribute("now", now);
        model.addAttribute("data", datas);
        return "chart";
    }

    @ResponseBody
    @RequestMapping("/getChartDatas")
    public List<Chart> getChartDatas(@RequestParam("date")String date) {
        List<Chart> charts = chartMapper.listChart(date);
        return charts;
    }
}
